#include <cstdio>
#include <iostream>
#include <string>
#define N 101
using namespace std;

int n, q;
string rig[N][2];
string che[N][5];

int main() {
	// freopen("testdata.in", "r", stdin);
	// freopen("testdata.out", "w", stdout);
	scanf("%d  %d", &n, &q);
	for (int i = 1; i <= n; ++i) {
		cin >> rig[i][0] >> rig[i][1];
	}
	string tmp,tmp2;
	int tmp3;
	for (int i = 1; i <= q; ++i) {
		cin >> tmp2;
		for (int k = 1; k <= n; ++k) {
			if (tmp2 == rig[k][0]) {
				tmp3 = k;
			}
		}
		for (int j = 0; j < 4; ++j) {
			cin >> tmp;
			if (tmp == rig[tmp3][1]) {
				cout << (char)('A' + j) << endl;
			}
		}
	}
	// fclose(stdin);
	// fclose(stdout);
	return 0;
}